<template>
  <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
    <el-form ref="form" :model="form" :rules="rules" label-width="140px">
      <el-form-item :label="$t('plan_type')"  prop="schemeType">
        <el-radio-group v-model="form.schemeType">
          <el-radio
            :label="parseInt(dict.value)"
            v-for="dict in dict.type.contribution_plan_type"
            :key="dict.value"
            >{{ dict.label }}</el-radio
          >
        </el-radio-group>
      </el-form-item>
      <el-form-item :label="$t('plan_name')" prop="schemeName">
        <el-input
          v-model="form.schemeName"
          :placeholder="$t('please_enter', { name: $t('plan_name') })"
        />
      </el-form-item>
      <el-row :gutter="10">
        <el-col :span="12">
          <el-form-item :label="$t('plan_code')" prop="schemeCode">
            <el-input
              v-model="form.schemeCode"
              :placeholder="$t('please_enter', { name: $t('plan_code') })"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('remarks')" prop="remark">
            <el-input
              v-model="form.remark"
              :placeholder="$t('please_enter', { name: $t('remarks') })"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('plan_number')" prop="schemeNum">
            <el-input
              v-model="form.schemeNum"
              :placeholder="$t('please_enter', { name: $t('plan_number') })"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('employer_name')" prop="employerId">
            <el-select
              v-model="form.employerId"
              filterable
              :placeholder="$t('please_select', { name: $t('employer_name') })"
            >
              <el-option
                v-for="item of employerList"
                :key="item.id"
                :label="item.employerName"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <template v-if="form.schemeType == 1">
        <el-form-item
          :label="$t('employer_participation_number')"
          prop="employerParticipationNumber"
        >
          <el-input
            v-model="form.employerParticipationNumber"
            :placeholder="
              $t('please_enter', { name: $t('employer_participation_number') })
            "
          />
        </el-form-item>

        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item :label="$t('contact_person_name')" prop="contactName">
              <el-input
                v-model="form.contactName"
                :placeholder="
                  $t('please_enter', { name: $t('contact_person_name') })
                "
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('contact_number')" prop="contactPhone">
              <el-input
                v-model="form.contactPhone"
                :placeholder="
                  $t('please_enter', { name: $t('contact_number') })
                "
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item :label="$t('address')" prop="address">
          <el-input
            v-model="form.address"
            :placeholder="$t('please_enter', { name: $t('address') })"
          />
        </el-form-item>
        <el-form-item :label="$t('effective_date')">
          <el-date-picker
            clearable
            v-model="form.effectiveDate"
            type="date"
            value-format="yyyy-MM-dd"
            :placeholder="$t('please_select', { name: $t('effective_date') })"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item
          :label="$t('part_of_voluntary_contribution')"
          prop="voluntaryContributionStatus"
        >
          <el-radio-group v-model="form.voluntaryContributionStatus">
            <el-radio
              v-for="dict in dict.type.contribution_voluntarily"
              :key="dict.value"
              :label="parseInt(dict.value)"
              >{{ dict.label }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
        <template v-if="form.voluntaryContributionStatus == 0">
          <el-row :gutter="10">
            <el-col :span="12">
              <el-form-item
                :label="$t('offset_with_employer_mpf')"
                prop="hedgeWithEmployerMpf"
              >
                <el-input
                  v-model="form.hedgeWithEmployerMpf"
                  :placeholder="
                    $t('please_enter', { name: $t('offset_with_employer_mpf') })
                  "
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item
                :label="$t('employer_contribution_ceiling')"
                prop="employerContributionCap"
              >
                <el-input
                  v-model="form.employerContributionCap"
                  :placeholder="
                    $t('please_enter', {
                      name: $t('employer_contribution_ceiling'),
                    })
                  "
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item
                :label="$t('offset_with_employee_mpf')"
                prop="hedgeWithEmployeeMpf"
              >
                <el-input
                  v-model="form.hedgeWithEmployeeMpf"
                  :placeholder="
                    $t('please_enter', { name: $t('offset_with_employee_mpf') })
                  "
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item
                :label="$t('employee_contribution_ceiling')"
                prop="employeeContributionCap"
              >
                <el-input
                  v-model="form.employeeContributionCap"
                  :placeholder="
                    $t('please_enter', {
                      name: $t('employee_contribution_ceiling'),
                    })
                  "
                />
              </el-form-item>
            </el-col>
          </el-row>

          <el-form-item
            :label="$t('employers_voluntary')"
            prop="contributionPeriod"
            label-width="300px"
          >
            <el-input v-model="form.contributionPeriod" />
          </el-form-item>
          <el-form-item
            :label="$t('continue_contribution_after')"
            prop="highestAge"
            label-width="300px"
          >
            <el-input v-model="form.highestAge" />
          </el-form-item>
          <el-form-item
            :label="$t('continue_contribution_below')"
            prop="belowLowest"
            label-width="300px"
          >
            <el-input v-model="form.belowLowest" />
          </el-form-item>
          <el-form-item
            :label="$t('effective_date')"
            prop="effectiveDateVoluntaryContributions"
          >
            <el-date-picker
              clearable
              v-model="form.effectiveDateVoluntaryContributions"
              type="date"
              value-format="yyyy-MM-dd"
              :placeholder="$t('please_select', { name: $t('effective_date') })"
            >
            </el-date-picker>
          </el-form-item>
        </template>
      </template>
      <template v-if="form.schemeType == 0">
        <el-form-item
          :label="$t('offset_with_employer_mpf')"
          prop="hedgeWithEmployerMpf"
        >
          <el-input
            v-model="form.hedgeWithEmployerMpf"
            :placeholder="
              $t('please_enter', { name: $t('offset_with_employer_mpf') })
            "
          />
        </el-form-item>
        <el-form-item :label="$t('remarks')" prop="hedgeWithEmployerMpfremark">
          <el-input
            v-model="form.hedgeWithEmployerMpfremark"
            :placeholder="$t('please_enter', { name: $t('remarks') })"
          />
        </el-form-item>
        <el-form-item
          :label="$t('offset_with_employee_mpf')"
          prop="hedgeWithEmployeeMpf"
        >
          <el-input
            v-model="form.hedgeWithEmployeeMpf"
            :placeholder="
              $t('please_enter', { name: $t('offset_with_employee_mpf') })
            "
          />
        </el-form-item>
        <el-form-item :label="$t('remarks')" prop="hedgeWithEmployeeMpfremark">
          <el-input
            v-model="form.hedgeWithEmployeeMpfremark"
            :placeholder="$t('please_enter', { name: $t('remarks') })"
          />
        </el-form-item>
        <el-form-item
          :label="$t('employer_contribution_ceiling')"
          prop="employerContributionCap"
        >
          <el-input
            v-model="form.employerContributionCap"
            :placeholder="
              $t('please_enter', { name: $t('employer_contribution_ceiling') })
            "
          />
        </el-form-item>
        <el-form-item
          :label="$t('employee_contribution_ceiling')"
          prop="employeeContributionCap"
        >
          <el-input
            v-model="form.employeeContributionCap"
            :placeholder="
              $t('please_enter', { name: $t('employee_contribution_ceiling') })
            "
          />
        </el-form-item>
        <el-form-item :label="$t('effective_date')" prop="effectiveDate">
          <el-date-picker
            clearable
            v-model="form.effectiveDate"
            type="date"
            value-format="yyyy-MM-dd"
            :placeholder="$t('please_select', { name: $t('effective_date') })"
          >
          </el-date-picker>
        </el-form-item>
      </template>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">{{
        $t("confirm")
      }}</el-button>
      <el-button @click="cancel">{{ $t("cancel") }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addScheme, updateScheme } from "@/api/custom/contributionPlan";
import { getAllEmployer } from "@/api/custom/employerList";
export default {
  name: "Scheme",
  dicts: [
    "contribution_plan_type",
    "common_status",
    "contribution_voluntarily",
  ],
  data() {
    return {
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        schemeName: [
          {
            required: true,
            message: this.$t("cannot_empty", { name: this.$t("plan_name") }),
            trigger: "blur",
          },
        ],
        employerId: [
          {
            required: true,
            message: this.$t("cannot_empty", { name: this.$t("employer_name") }),
            trigger: "change",
          },
        ],
        schemeCode: [
          {
            required: true,
            message: this.$t("cannot_empty", { name: this.$t("plan_code") }),
            trigger: "blur",
          },
        ],
      },
      employerList: [],
      title: "",
      open: false,
    };
  },
  created() {
    this.getEmployerList();
  },
  methods: {
    getEmployerList() {
      getAllEmployer().then((res) => {
        this.employerList = res.data;
      });
    },
    reset() {
      this.form = {
        id: null,
        schemeType: 1,
        schemeName: null,
        schemeCode: null,
        employerName: null,
        employerParticipationNumber: null,
        contactName: null,
        contactPhone: null,
        address: null,
        effectiveDateVoluntaryContributions: null,
        voluntaryContributionStatus: 1,
        schemeNum: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,

        hedgeWithEmployerMpf: null,
        hedgeWithEmployerMpfremark: null,
        hedgeWithEmployeeMpf: null,
        hedgeWithEmployeeMpfremark: null,
        employerContributionCap: null,
        employeeContributionCap: null,
        contributionPeriod: null,
        highestAge: null,
        belowLowest: null,
        effectiveDateVoluntaryContributions: null,
      };
      this.resetForm("form");
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.schemeType == 0) {
            this.form.voluntaryContributionStatus = 1;
          }
          if (this.form.id != null) {
            updateScheme(this.form).then((response) => {
              this.$modal.msgSuccess(this.$t("operation_successful"));
              this.open = false;
              this.$emit("handleSuccess");
            });
          } else {
            addScheme(this.form).then((response) => {
              this.$modal.msgSuccess(this.$t("operation_successful"));
              this.open = false;
              this.$emit("handleSuccess");
            });
          }
        }
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    handleAdd(sign) {
      this.reset();
      this.open = true;
      this.title = this.$t("add");
    },
    handleUpdate(rows) {
      this.reset();
      rows.voluntaryContributionStatus = Number(
        rows.voluntaryContributionStatus
      );
      rows.schemeType = Number(rows.schemeType);
      this.form = rows;
      this.open = true;
      this.title = this.$t("modify");
    },
  },
};
</script>

<style lang="scss" scoped></style>
